﻿using Sabloane_de_proiectare.Factory;
using Sabloane_de_proiectare.Memento;
using Sabloane_de_proiectare.Observable;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Input;

namespace Sabloane_de_proiectare
{
    public class GridElements
    {
        public ObservableCollection<Container> Containers {get; set;}
        public MementoStates memento { get; set; }
        private RailFactory railFactory;
        private ObservableTemplate observed;
        public GridElements() {
            railFactory = new RailFactory ();
            Containers = new ObservableCollection<Container>();
            memento = new MementoStates();
            for (int i = 0; i < 10; i++)
                for (int j = 0; j < 10; j++)
                {
                    Container temp = new Container(i,j);
                    temp.Tapped += EventHandler;
                    Containers.Add(temp);
                }
        }
        public void setObserver(ObservableTemplate t)
        {
            observed = t;
        }
        private void EventHandler(object obj, TappedRoutedEventArgs e)
        {
            memento.saveState(this);
            Container sender = obj as Container;
            int addedItemType;
            addedItemType = sender.setChildren( railFactory.createNextItem(sender.getCurrentChild()), railFactory.getChildrenType());
            observed.isAdded(sender.Row, sender.Column,addedItemType);
        }
      
        public Container getElementAt(int Row, int Column)
        {
            int i;
            for (i = 0; i < Containers.Count; i++)
                if ((Containers.ElementAt(i).Row == Row) && (Containers.ElementAt(i).Column == Column))
                    break;
            return Containers.ElementAt(i);
        }
    }
}
